<script setup lang="ts">
import { ref } from "vue";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import ReCol from "@/components/ReCol";
const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    title: "",
    status: 0,
    Id: 0,
    dispatchOrderId: 0,
    dateTime: null,
    remarks: ""
  })
});
const statusOptions = [
  {
    value: 1,
    label: "有效"
  },
  {
    value: 2,
    label: "无效"
  },
  {
    value: 3,
    label: "已完成"
  }
];
const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}
defineExpose({ getRef });
function setRangeTime() {
  return true;
}
import Empty from "./utils/empty.svg?component";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import AddFill from "@iconify-icons/ep/plus";
import Delete from "@iconify-icons/ep/delete";
const dataList = ref([]);
const columns: TableColumnList = [
  {
    label: "发货单",
    prop: "dispatchOrderGoodsId",
    slot: "goods"
  },
  {
    label: "库房",
    prop: "warehouseId",
    slot: "warehouse"
  },
  {
    label: "库位",
    prop: "goodsLocationId",
    slot: "location"
  },
  {
    label: "操作",
    fixed: "right",
    width: 90,
    slot: "operation"
  }
];

const selectValue = ref([]);
function onAdd() {
  selectValue.value.push({
    goodsSelectValue: "",
    warehouseSelectValue: ""
  });
  dataList.value.push({
    did: dataList.value.length + 1,
    dispatchOrderGoodsId: 0,
    warehouseId: 0,
    goodsLocationId: 0
  });
}

function onDel(row) {
  const index = dataList.value.indexOf(row);
  if (index !== -1) {
    dataList.value.splice(index, 1);
    selectValue.value.splice(index, 1);
  }
}

//下拉选项商品列表
import { useGoodsColumns } from "./utils/goodsColumns";
const goodsSelectRef = ref();
const {
  goodsColumns,
  goodsDataList,
  goodsPagination,
  goodsSelectValue,
  goodsOnRowClick
} = useGoodsColumns(goodsSelectRef, dataList.value, selectValue.value);

//下拉选择仓库列表
import { useWarehouseColumns } from "./utils/warehouseColumns";

const warehouseSelectRef = ref();
const {
  warehouseColumns,
  warehousePagination,
  warehouseSelectValue,
  warehouseDataList,
  warehouseRowStyle,
  warehouseOnRowClick
} = useWarehouseColumns(warehouseSelectRef, dataList.value, selectValue.value);
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="auto"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="发货订单id" prop="dispatchOrderId">
          <el-input
            v-model="newFormInline.dispatchOrderId"
            clearable
            placeholder="请选择发货订单id"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24" prop="dateTime">
        <el-form-item label="存货日期">
          <el-date-picker
            v-model="newFormInline.dateTime"
            type="daterange"
            class="!w-[240px]"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :popper-options="{
              placement: 'bottom-start'
            }"
            value-format="YYYY-MM-DD"
            @change="setRangeTime()"
          />
        </el-form-item>
      </re-col>
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="newFormInline.status"
            placeholder="请选择状态"
            class="w-full"
            clearable
          >
            <el-option
              v-for="(item, index) in statusOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </re-col>
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item label="存货备注" prop="remarks">
          <el-input
            v-model="newFormInline.remarks"
            placeholder="日程详情"
            type="textarea"
          />
        </el-form-item>
      </re-col>
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item label="详情信息" prop="goodsList">
          <pure-table
            row-key="did"
            align-whole="center"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            :data="dataList"
            :columns="columns"
          >
            <template #empty>
              <Empty fill="var(--el-svg-monochrome-grey)" class="m-auto" />
            </template>
            <template #append>
              <el-button
                plain
                class="w-full my-2"
                :icon="useRenderIcon(AddFill)"
                @click="onAdd"
              >
                添加一行数据
              </el-button>
            </template>
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :icon="useRenderIcon(Delete)"
                @click="onDel(row)"
              />
            </template>
            <template #goods="scope">
              <el-select
                ref="goodsSelectRef"
                v-model="selectValue[scope.row.did - 1].goodsSelectValue"
                class="!w-[240px]"
                placeholder="请选择"
                value-key="id"
                clearable
              >
                <template #empty>
                  <div class="m-4">
                    <pure-table
                      row-key="id"
                      alignWhole="center"
                      :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                      }"
                      :pagination="goodsPagination"
                      :data="goodsDataList"
                      :columns="goodsColumns"
                      @row-click="
                        (row, column, event) => {
                          goodsOnRowClick(row, column, event, scope.row.did);
                        }
                      "
                    />
                  </div>
                </template>
              </el-select>
            </template>
            <template #warehouse="scope">
              <el-select
                ref="warehouseSelectRef"
                v-model="selectValue[scope.row.did - 1].warehouseSelectValue"
                class="!w-[200px]"
                placeholder="请选择"
                value-key="id"
                clearable
              >
                <template #empty>
                  <div class="m-4">
                    <pure-table
                      row-key="id"
                      alignWhole="center"
                      :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                      }"
                      :row-style="warehouseRowStyle"
                      :data="warehouseDataList"
                      :columns="warehouseColumns"
                      :pagination="warehousePagination"
                      @row-click="
                        (row, column, event) => {
                          warehouseOnRowClick(
                            row,
                            column,
                            event,
                            scope.row.did
                          );
                        }
                      "
                    />
                  </div>
                </template>
              </el-select>
            </template>
            <template #location>
              <el-select
                ref="selectRef"
                class="!w-[180px]"
                placeholder="请选择"
                value-key="corpId"
                clearable
              >
                <template #empty>
                  <div class="m-4">
                    <pure-table
                      row-key="corpId"
                      alignWhole="center"
                      :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                      }"
                      :data="dataList"
                      :columns="columns"
                    />
                  </div>
                </template>
              </el-select>
            </template>
          </pure-table>
        </el-form-item>
      </re-col>
      <re-col :value="24" :xs="24" :sm="24">
        <code>
          <pre> {{ dataList }}</pre>
          ;
        </code>
      </re-col>
    </el-row>
  </el-form>
</template>
<style scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}
</style>
